package com.tbit.main.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.common.utils.TraceUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.MachineTag;
import com.tbit.main.pojo.TagBaseInfo;
import com.tbit.main.pojo.entity.TagAndMachineBind;
import com.tbit.main.service.MachineTagService;
import com.tbit.main.service.TagService;
import com.tbit.main.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @description: 车辆和标签的绑定关系
 * @author: mo.shanyong
 * @date: 2024/11/22 17:13
 */
@Slf4j
@RestController
@RequestMapping("/machineTag")
public class MachineTagController  {
    @Autowired
    private MachineTagService machineTagService;

    /**
     * @description: 添加车辆标签绑定
     * @author: mo.shanyong
     * @date: 2024/11/25 16:14
     * @param: machineIdList 车辆Id
     * @param: tagIdList 要绑定的标签id
     * @return: com.tbit.main.common.JsonResult
     */
    @RequestMapping("/addTag")
    public JsonResult addTag(@RequestParam List<Integer> machineIdList,@RequestParam List<Integer> tagIdList){
        Assert.notNull(machineIdList,"添加标签的车辆id为空");
        Assert.notNull(tagIdList,"添加标签的标签id为空");
        AccountUser accountUser = LoginUserContextHolder.getLoginUser();
        log.info("addTag---->machineIdList:{},tagIdList:{},accountUser:{}",
                JSON.toJSONString(machineIdList),JSON.toJSONString(tagIdList),JSON.toJSONString(accountUser));
        machineTagService.bindTagBatch(machineIdList,tagIdList,accountUser);
        return JsonResult.succ();
    }

    /**
     * @description: 取消绑定的车辆标签
     * @author: mo.shanyong
     * @date: 2024/11/25 16:47
     * @param: machineIdList 车辆id
     * @param: tagIdList 标签Id
     * @return: com.tbit.main.common.JsonResult
     */
    @RequestMapping("/cancelTag")
    public JsonResult cancelTag(@RequestParam List<Integer> machineIdList,@RequestParam List<Integer> tagIdList){
        if (CollectionUtils.isEmpty(machineIdList)){
            return JsonResult.fail(null,"要编辑的车辆id为空", TraceUtil.getTraceIdByLocal());
        }
        if (CollectionUtils.isEmpty(tagIdList))
            return JsonResult.succ();
        AccountUser accountUser = LoginUserContextHolder.getLoginUser();
        log.info("cancelTag----->machineIdList:{},tagIdList:{},accountUser:{}",
                JSON.toJSONString(machineIdList),JSON.toJSONString(tagIdList),JSON.toJSONString(accountUser));
        machineTagService.cancelTagBatch(machineIdList,tagIdList,accountUser);
        return JsonResult.succ();
    }

    /**
     * @description: 运维端管理标签，直接使用传进来的标签直接进行覆盖已有的
     * @author: mo.shanyong
     * @date: 2024/12/15 14:43
     * @param: machineId 车辆id
     * @param: tagIdList 标签id
     * @return: com.tbit.main.common.JsonResult
     */
    @RequestMapping("/manageTag")
    public JsonResult manageTag(Integer machineId,@RequestParam List<Integer> tagIdList){
        Assert.notNull(machineId,"车辆id为空");
        AccountUser accountUser = LoginUserContextHolder.getLoginUser();
        log.info("machineId:{},tagIdList:{}",machineId,JSON.toJSONString(tagIdList));
        machineTagService.manageTag(machineId, tagIdList,accountUser);
        return JsonResult.succ();
    }
}
